#!/usr/bin/python
'''
*************************************************************************************************************************************************
Hypergraph input file generator
Version: 1.0
Author: Victor Codocedo
Description: This script generates an rcf file of context for coron
*************************************************************************************************************************************************
'''
import psycopg2
myconnection = "dbname='sa' user='ecos' host='localhost' password='ecos'"

conn = psycopg2.connect(myconnection)
cur = conn.cursor()
limit = 50
sql = "select concept.label,identifier, pub_year from concept, pub_concept, publication where concept.id = id_concept and publication.id = id_publication order by identifier"
sql = "select concept.label,identifier, pub_year from concept, pub_concept, publication where concept.id = id_concept and publication.id = id_publication and concept.id in (select id from (select concept.id, count(id_publication) cnt from concept,pub_concept where id=id_concept group by concept.id order by cnt DESC limit %d) as foo)"%limit
#sql = "select auth_name,concept.label from concept, pub_concept, publication, pub_auth, author where concept.id = id_concept and publication.id = pub_concept.id_publication and pub_auth.id_publication = publication.id and author.id=id_author order by label"
table = []

cur.execute(sql)
table.append(cur.fetchall())
context = {}
concepts = {}
conceptsa = []
attr = {}
attra = []

for rows in table:
    for r in rows:
        ct = r[0]
        attrs = [ r[1] ]
        
        if ct not in concepts:
            #print ct
            concepts[ct] = len(concepts)
            conceptsa.append(ct)
        
        if ct not in context:
            context[ct] = {}
        for at in attrs:
            if at not in attr:
                attr[at] = len(attr)
                attra.append(at)
                print at
            if attr[at] not in context[ct]:
                #context[ct].append(attr[at])
                context[ct][attr[at]] = 1
            else:
                context[ct][attr[at]] += 1
                print context[ct][attr[at]]
                #print "c"+at +"/"+ str(attr[at])
#print attr
print str(len(attr)) + " atributos encontrados"
print str(len(concepts)) + " elementos encontrados"
#for c in context:
#    print c +":"+ str(context[c])
salida = "[Relational Context]\nDefault Name\n[Binary Relation]\nName_of_dataset\n"
for c in conceptsa:
    salida += str(c) + " | "
salida += "\n"
for c in attra:
    salida += str(c) + " | "
salida += "\n"
#salida binaria
salida_bin = salida
for c in conceptsa:
    for i in range(len(attr)):
        if i in context[c]:
            salida += str(context[c][i])+" "
            salida_bin += "1 "
        else:
            salida += "0 "
            salida_bin += "0 "
    salida += "\n"
    salida_bin += "\n"
salida += "[END Relational Context]"
salida_bin += "[END Relational Context]"
fb = open("contexto_bin.rcf","w")
f = open("contexto.rcf","w")
fb.write(salida_bin);
f.write(salida);
f.close()
fb.close()

cur.close()
conn.close()
